//
//  MenuFifthViewController.m
//  土巴兔
//
//  Created by 千锋 on 16/1/7.
//  Copyright (c) 2016年 IOS1503薛凯. All rights reserved.
//

#import "MenuFifthViewController.h"
#import "Default.h"
#import "MenuZxzsModel.h"
#import "MenuSpxzxModel.h"
#import "MenuNnxjModel.h"
#import "MenuXfkjModel.h"
#import "MenuZxzsTableViewCell.h"
#import "MenuSpxzxTableViewCell.h"
#import "MenuNnxjTableViewCell.h"
#import "MenuXfkjTableViewCell.h"
@interface MenuFifthViewController () <UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic)UITableView* tableview;
@property(strong,nonatomic)NSMutableArray* zxzsData;
@property(strong,nonatomic)NSMutableArray* spxzxData;
@property(strong,nonatomic)MenuNnxjModel* nnxjData;
@property(strong,nonatomic)NSMutableArray* xfkjData;
@property (strong,nonatomic)UIView* headview;
@end

@implementation MenuFifthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden=YES;
    // Do any additional setup after loading the view from its nib.
    //[self createWebView];
    [self createHeadview];
    [self getData];
    [self createTableView];
    
}
- (void)createHeadview{
    _headview=[[UIView alloc] initWithFrame:CGRectMake(0, 0, DEVICEWIDTH, 60)];
    [self.view addSubview:_headview];
    
    UIButton* back=[[UIButton alloc] initWithFrame:CGRectMake(15, 30, 12, 15)];
    [back setBackgroundImage:[UIImage imageNamed:@"bbi_back"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [_headview addSubview:back];
    
    UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(70, 20, 150, 30)];
    label.text=@"装修攻略";
    [_headview addSubview:label];
}
- (void)backClick{
    [self.navigationController popToRootViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden=NO;
}
- (void)createTableView{
    _tableview=[[UITableView alloc] initWithFrame:CGRectMake(0, 60, DEVICEWIDTH, DEVICEHEIGHT-60)];
    _tableview.dataSource=self;
    _tableview.delegate=self;
    [self.view addSubview:_tableview];
    [_tableview registerClass:[MenuZxzsTableViewCell class] forCellReuseIdentifier:@"MenuZxzsTableViewCell"];
    [_tableview registerClass:[MenuSpxzxTableViewCell class] forCellReuseIdentifier:@"MenuSpxzxTableViewCell"];
    [_tableview registerClass:[MenuNnxjTableViewCell class] forCellReuseIdentifier:@"MenuNnxjTableViewCell"];
    [_tableview registerClass:[MenuXfkjTableViewCell class] forCellReuseIdentifier:@"MenuXfkjTableViewCell"];
}
#pragma mark- 表格协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return 4;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        MenuZxzsTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"MenuZxzsTableViewCell"];
        [cell relayoutWithData:self.zxzsData];
        return cell;
    }else if (indexPath.row==1){
        MenuSpxzxTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"MenuSpxzxTableViewCell"];
        [cell relyoutWithData:_spxzxData];
        return cell;
    }else if(indexPath.row==2){
        MenuNnxjTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"MenuNnxjTableViewCell"];
        [cell relayoutWithData:_nnxjData];
        return cell;
    }else if (indexPath.row==3){
        MenuXfkjTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"MenuXfkjTableViewCell"];
        [cell relayoutWithData:_xfkjData];
        return cell;
    }
    return nil;
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        return 200;
    }else if(indexPath.row==1){
        return 310;
    }else if(indexPath.row==2){
        return 240;
    }else if (indexPath.row==3){
        return DEVICEWIDTH*2/3+75;
    }
    return 30;
}
- (void)getData{
    self.zxzsData=[[NSMutableArray alloc] initWithCapacity:0];
    self.spxzxData=[[NSMutableArray alloc] initWithCapacity:0];
    self.xfkjData=[[NSMutableArray alloc] initWithCapacity:0];
    NSURL* url=[NSURL URLWithString:@"http://mobileapi.to8to.com/index.php"];
    NSMutableString* reqHead=[NSMutableString stringWithString:@"systemversion=22&imei=569eb1056c38e1e8&appversion=3.6.0&appostype=1&action=index&version=2.5&appid=15&uid="];
    NSString* tail1=@"&to8to_token=";
    [reqHead appendString:[[NSUserDefaults standardUserDefaults] objectForKey:@"uid"]];
    [reqHead appendString:tail1];
    [reqHead appendString:[[NSUserDefaults standardUserDefaults] objectForKey:@"to8to_token"]];
    NSString* tail2=@"&channel=%E9%AD%85%E6%97%8F%E5%BA%94%E7%94%A8%E4%B8%AD%E5%BF%83&model=video&";
    [reqHead appendString:tail2];
    NSData* data=[reqHead dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest* request=[NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:data];
    
    [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSDictionary* dic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary* next=[dic objectForKey:@"data"];
        
        NSArray* zxzsarr=[next objectForKey:@"zxzs"];
        for (NSDictionary* item in zxzsarr) {
            MenuZxzsModel* model=[[MenuZxzsModel alloc] initWithDic:item];
            [self.zxzsData addObject:model];
            [self.tableview reloadData];
        }
        NSArray* spxzxarr=[next objectForKey:@"spxzx"];
        for (NSDictionary* item2 in spxzxarr) {
            MenuSpxzxModel* model2=[[MenuSpxzxModel alloc] initWithDic:item2];
            [self.spxzxData addObject:model2];
            [self.tableview reloadData];
        }
        NSDictionary* nnxjdic=[next objectForKey:@"nnxj"];
        self.nnxjData=[[MenuNnxjModel alloc] initWithDic:nnxjdic];
        [self.tableview reloadData];
        NSArray* xfkjarr=[next objectForKey:@"xfkj"];
        for (NSDictionary* item3 in xfkjarr) {
            MenuXfkjModel* model3=[[MenuXfkjModel alloc] initWithDic:item3];
            [self.xfkjData addObject:model3];
            [self.tableview reloadData];
        }
    }] resume];
}
@end
